import pymysql


# 使用面向对象的形式来拿到值
class Dept(object):
    def __init__(self,stu_id,stu_name,stu_stuaddr):
        self.stu_id=stu_id
        self.stu_name=stu_name
        self.stu_stuaddr=stu_stuaddr
    def __str__(self):
        # 使用f''的形似来代替format使用，不需要一个个的format，f''能直接把中间的{}的内容直接引用进来
        return f'{self.stu_id}\t{self.stu_name}\t{self.stu_stuaddr}'
# cursorclass=pymysql.cursors.DictCursor更换游标的类型
def main():
    connect = pymysql.connect(host="39.107.245.102", port=3306, user="root", passwd="wuji5201314.", db="school",
                              charset="utf8",cursorclass=pymysql.cursors.DictCursor)
    try:
        with connect.cursor() as cursor:
            # 查询语句不进行赋值
            cursor.execute("select stu_id,stu_name,stu_stuaddr from tb_student")
            for row in cursor.fetchall():
                # 使用**变量名的格式接受可变长的字典变量
                dept=Dept(**row)
                print(dept)
    except pymysql.MySQLError as error:
        print('查询失败.{}'.format(error))
    # finally:正常异常都执行，必须要释放资源
    finally:
        connect.close()
if __name__ == "__main__":
    main()